from flask import Flask, render_template, url_for, session, redirect, request
from . import api
from ..models import User, Article, Attention, db


@api.route('/like/<int:id>', methods=["GET", "POST"])
def like(id):
    if request.method == "GET":
        ar = Article.query.filter_by(id=id).first()
        ar.arc_click = ar.arc_click + 1
        db.session.commit()
        print(str(ar.arc_click))
        return str(ar.arc_click)


@api.route('/attention/<int:id>', methods=["GET", "POST"])
def attention(id):
    attentions = []
    username = session['username']
    if request.method == "GET":
        ar = Article.query.filter_by(id=id).first()
        us = Attention.query.filter_by(user_name=username).all()
        for item in us:
            attentions.append(item.attention_user)
        if ar.arc_author in attentions:

            return '已经关注'
        else:
            attentionH = Attention(user_name=username, attention_user=ar.arc_author)
            db.session.add(attentionH)
            db.session.commit()
            return '多谢关注'

